<template>
	<view class="loginview">
		<view class="logo-box">
			<view class="logo">
				<image src="/static/logo.png" mode="widthFix"></image>
				<h1 class="h1">视频会议</h1>
			</view>
		</view>
		
		<view class="form-box">
			<u-form :model="form" ref="uForm" :error-type="errorType">
				<u-form-item class="form-item" prop="name"><u-input class="form-input" label-width="0" v-model="form.name" type="text" placeholder="请输入id" /></u-form-item>
				<u-form-item class="form-item"><u-input class="form-input" label-width="0" v-model="form.nickname" type="text" placeholder="请输入昵称" /></u-form-item>
				<u-form-item class="form-item" prop="password"><u-input class="form-input" label-width="0" v-model="form.password" type="password" placeholder="请输入密码" /></u-form-item>
				<u-form-item class="form-item" prop="password2"><u-input class="form-input" label-width="0" v-model="form.password2" type="password" placeholder="请再次输入密码" /></u-form-item>
			</u-form>
			<view class="form-text">
				<navigator url="/pages/login/login">马上登录</navigator>
				<navigator url="/pages/login/login">忘记密码</navigator>
			</view>
		</view>
		
		<view class="fiexd-btm"><u-button type="primary" @tap="register">注册</u-button></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: null,
					nickname: null,
					password: null,
					password2: null
				},
				rules: {
					name: [{
						required: true,
						message: '请输入id',
						trigger: 'change'
					}],
					password: [{
						required: true,
						min: 7,
						message: '密码不能少于7个字符',
						trigger: 'change'
					}],
					password2: [{
						required: true,
						min: 7,
						message: '密码不能少于7个字符',
						trigger: 'change'
					}, {
						// 自定义验证函数，
						validator: (rule, value, callback) => {
							return this.form.password == this.form.password2;
						},
						message: '两次输入密码不一致',
						trigger: 'change'
					}]
				},
				errorType: ['toast', 'border-bottom']
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {
			console.log('onLoad')
		},
		methods: {
			register() {
				this.$refs.uForm.validate(valid => {
					if (!valid) {
						console.log('验证失败')
						return
					} else {
						let options = {
							apiUrl: this.$im.config.apiURL,
							appKey: this.$im.config.appkey,
							username: this.form.name,
							password: this.form.password,
							nickname: this.form.nickname,
							success: (res) => {
								this.$helper.toast('注册成功！')
								let data = {
									apiUrl: this.$im.config.apiURL,
									appKey: this.$im.config.appkey,
									user: this.form.name,
									pwd: this.form.password,
									grant_type: "password"
								}
								uni.setStorage({
									key: "myUsername",
									data: data.user
								})
								uni.setStorage({
									key: 'myPassword',
									data: data.pwd
								})
								uni.navigateTo({
									url: '/pages/login/login'
								})
							},
							error: (res) => {
								if (res.statusCode !== "200") {
									if (res.statusCode == '400' && res.data.error == 'illegal_argument') {
										this.$helper.toast('用户名非法！')
										return
									}
									this.$helper.toast('用户名已被占用！')
								}
							}
						}
						this.$im.conn.registerUser(options)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.logo-box {
	padding: 100upx $uni-page-padding 30upx;
	text-align: center;

	.h1 {
		margin-bottom: 30upx;
		font-size: 48upx;
		font-weight: normal;
		color: #09f;
	}

	image {
		margin: 0 auto 20upx;
		display: block;
		width: 140upx;
		height: 140upx;
		border-radius: 140upx;
	}
}

.form-box {
	padding: $uni-page-padding;
	
	.form-item {
		display: flex;
		margin-bottom: 30upx;
		padding: 10upx 20upx;
		flex-wrap: nowrap;
		border: 1px solid #09f;
		
		.u-form-item {
			padding: 0;
			width: 100%;
		}
	}

	.form-text {
		display: flex;
		justify-content: space-between;
		font-size: 24upx;
		color: #09f;
	}
}
</style>
